﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.EstRec;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using FileOps;
using System.Data;

namespace BAL.EstRec.SYNC
{
    public delegate void EventHandler<SyncEventArgs>(object sender, SyncEventArgs e);

    public class Sync
    {
        public static bool uncompressZip(string zipFilePath, string targetFolder)
        {
            bool ret = true;
            try
            {
                if (File.Exists(zipFilePath))
                {
                    string baseDirectory = targetFolder;

                    using (ZipInputStream ZipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
                    {
                        ZipEntry theEntry;
                        while ((theEntry = ZipStream.GetNextEntry()) != null)
                        {
                            if (theEntry.IsFile)
                            {

                                if (theEntry.Name != "")
                                {
                                    string strNewFile = @"" + baseDirectory + @"\" + theEntry.Name;
                                    if (File.Exists(strNewFile))
                                    {
                                        continue;
                                    }

                                    using (FileStream streamWriter = File.Create(strNewFile))
                                    {

                                        int size = 4096;
                                        byte[] data = new byte[4096];
                                        while (true)
                                        {
                                            size = ZipStream.Read(data, 0, data.Length);
                                            if (size > 0)
                                                streamWriter.Write(data, 0, size);
                                            else
                                                break;
                                        }//while
                                        streamWriter.Close();
                                    }//using
                                }//if
                            }//using
                            else if (theEntry.IsDirectory)
                            {

                                string strNewDirectory = @"" + baseDirectory + @"\" + theEntry.Name;
                                if (!Directory.Exists(strNewDirectory))
                                {
                                    Directory.CreateDirectory(strNewDirectory);
                                }
                            }//else-if
                        }
                        ZipStream.Close();
                    }//using
                }
                else
                {
                    Console.WriteLine("*** El archivo Zip no existe en la carpeta Temporal.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("*** Error al descomprimir: " + ex.Message);
                ret = false;
            }
            return ret;
        }  //////////

        public static bool upsertControlTable(long unidFile, string fileName, string fileType, bool isUnzipped, bool idUploadedStg, bool isUploadedTbl)
        {
            bool done = false;
            string query = "SP_ER_UPSERT_LOG ";
            int result = 0;

            try
            {
                //if (!(unidFile > 0))
                //    return false;
                //else 
                query += "@unidFile=" + unidFile.ToString();

                if (!String.IsNullOrEmpty(fileName))
                    query += ", @fileName='" + fileName + "'";

                if (!String.IsNullOrEmpty(fileType))
                    query += ", @fileType='" + fileType + "'";

                query += ", @isUnzipped='" + isUnzipped
                    + "', @idUploadedStg = '" + idUploadedStg
                    + "', @isUploadedTbl = '" + isUploadedTbl + "'";

                result = SrvDB.ExecuteNonQuery(query);
                done = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return done;

        }////////


        public static bool upsertCatZipStatus(long unidCatZip, string zipName, bool isDownloaded, bool isUnzipped)
        {
            //
            bool done = false;
            int result = 0;

            try
            {
                string query = "SP_ER_UPSERT_CTRL_CATZIPFILES " + unidCatZip
                    + ", '" + zipName
                    + "','" + isDownloaded + "'"
                    + ",'" + isUnzipped + "'";

                result = SrvDB.ExecuteNonQuery(query);
                done = true;

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return done;
        }//

        public static bool upsertCatFileStatus(int idCatfile, long unidCatZip, string catFileName, bool isUploaded)
        {
            bool done = false;
            int result = 0;

            string comparer = catFileName.Split('\\').Last();

            try
            {
                string query = "SP_ER_UPSERT_CTRL_CATFILES " + idCatfile
                    + ", " + unidCatZip
                    + ", '" + catFileName
                    + "', '" + comparer
                    + "','" + isUploaded.ToString() + "'";

                result = SrvDB.ExecuteNonQuery(query);
                done = true;

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return done;
        }//

        public static DataTable getLastSyncData()
        {
            string query = "SP_ER_GET_LASTSYNCDATA";
            DataTable dt = new DataTable();

            try
            {
                dt = SrvDB.ExecuteQuery(query);
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return dt;
        }

        public static bool upsertSrchSyncLog()
        {
            bool done = false;
            int result = 0;
            string query = "SP_UPSERT_SRCHSYNCLOG";

            try
            {
                result = SrvDB.ExecuteNonQuery(query);
                done = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return done;
        }//

        public static DataTable getLastCheckupdate()
        {
            string query = "SP_ER_CHECK_DATESYNC";
            bool done = false;
            DataTable dt = new DataTable();

            try
            {
                dt = SrvDB.ExecuteQuery(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }


    }// end class
}
